package huhx.linux.com.kotlinlearn.utils

import android.util.Log


/**
 * Created by huhx on 2018-03-17 下午 4:26.
 */
object ServiceCacheUtils {
    private val LOG_TAG = ServiceCacheUtils::class.java.simpleName

    val map: MutableMap<String, Any> = mutableMapOf()

    /*
     * 从缓存中得到服务
     */
    fun <T> getService(tClass: Class<T>): T {
        val name = tClass.simpleName
        val value = map[name]
        Log.i(LOG_TAG, "object = " + value)
        if (value != null) {
            return value as T
        } else {
            val serviceClass = RetrofitUtils.getRetrofit()!!.create(tClass)
            map.put(name, serviceClass!!)
            return serviceClass
        }
    }

    /*
     * 将服务放进缓存中
     */
    fun putService(value: Any): Boolean {
        val name = value.javaClass.simpleName
        if (map[name] != null) {
            return false
        }
        map.put(name, value)
        return true
    }
}